@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: Check command line
ECHO.%* | FIND "?" >NUL
IF NOT ERRORLEVEL 1 GOTO Syntax
IF [%2]==[] GOTO Syntax

:: Keep variables local
SETLOCAL

:: Read variables from command line
SET RegSection=%~1
SET RegKey=%~2
SET RegVal=

:: Store content of registry section in temporary file
REGEDIT /E "%Temp%.\readreg.dat" "%~1"

:: Use EGREP to search requested value in temporary file
FOR /F "tokens=1* delims==" %%A IN ('TYPE "%Temp%.\readreg.dat" 2^>NUL ^| EGREP -i "^^\"?%~2\"?="') DO SET RegVal=%%B

:: Format and display the result
IF DEFINED RegVal (
	SET RegVal=%RegVal:\\=\%
)
IF DEFINED RegVal (
	ECHO.
	ECHO [%RegSection%]
	ECHO %RegKey%=%RegVal%
)

:: Delete temporary file
IF EXIST "%Temp%.\readreg.dat" DEL "%Temp%.\readreg.dat"

:: Done
ENDLOCAL & SET RegVal=%RegVal%
GOTO:EOF


:Syntax
ECHO.
ECHO ReadReg.bat,  Version 1.02 for Windows NT 4 / 2000 / XP
ECHO Read a value from the registry
ECHO.
ECHO Usage:  READREG  "section"  "key"
ECHO.
ECHO Where:           "section"  is the section name, without brackets
ECHO                  "key"      is the key whose value must be read
ECHO.
ECHO This batch file uses EGREP to search for the value
ECHO.
ECHO Example:
ECHO READREG "HKEY_CURRENT_USER\Environment" "path"
ECHO should return the user part of the PATH variable
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
